package com.bootdo.hotel.service.impl;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.Hog;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.hotel.domain.HotelRoomDayStatus;
import com.bootdo.hotel.service.HotelRoomDayStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.bootdo.hotel.dao.HotelOrderDao;
import com.bootdo.hotel.domain.HotelOrder;
import com.bootdo.hotel.service.HotelOrderService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class HotelOrderServiceImpl implements HotelOrderService {

	private static Lock LOCK = new ReentrantLock();

	@Autowired
	private HotelOrderDao hotelOrderDao;

	@Autowired
	private HotelRoomDayStatusService hotelRoomDayStatusService;
	
	@Override
	public HotelOrder get(Long id){
		return hotelOrderDao.get(id);
	}

	@Override
	public HotelOrder getByMobileByInRoom(String mobile) {
		return hotelOrderDao.getByMobileByInRoom(mobile);
	}

	@Override
	public List<HotelOrder> list(Map<String, Object> map){
		return hotelOrderDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return hotelOrderDao.count(map);
	}
	
	@Override
	public int save(HotelOrder hotelOrder){
	
		if(hotelOrder.getId() != null) {
			return hotelOrderDao.update(hotelOrder);
		}else {
			return hotelOrderDao.save(hotelOrder);
		}		
	
		
	}
	
	@Override
	public int update(HotelOrder hotelOrder){
		return hotelOrderDao.update(hotelOrder);
	}
	
	@Override
	public int remove(Long id){
		return hotelOrderDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return hotelOrderDao.batchRemove(ids);
	}

	@Override
	@Transactional
	public int saveOrderAndDayStatus(HotelOrder hotelOrder) {

		/*if(LOCK.tryLock()){
			try {*/

				//得到预订之间的价格
				List<Date> dateList =  DateUtils
						.getStartTimeAndEndTimeEveryDay(
								hotelOrder.getInDate(),
								hotelOrder.getOutDate());
				AtomicBoolean isUpdateOk = new AtomicBoolean(true);
				dateList.forEach(item ->{
					String day = DateUtils.format(item);
					int status = 3;
					if(hotelOrder.getStatus().intValue() == 2
							|| hotelOrder.getStatus().intValue() == 6
							|| hotelOrder.getStatus().intValue() <= 0
					){
						status = 1;
					}
					//微信预定
					if(hotelOrder.getStatus().intValue() == 0 && !hotelOrder.getFlag().equals("DUO")){
						status = 5;
						int finalStatus = status;
						hotelOrder.getHotelRooms().stream().forEach(hotelRoom ->{
							saveStatus(hotelRoom.getHotelId(),hotelRoom.getId(),hotelRoom.getTypeId(),day,finalStatus,isUpdateOk);
						});
					}else{
						//房态等于1说明有人退房
						if(status == 1){
					/*
					// 郝江波改
					if(hotelOrder.getStatus().intValue() <= 0){
						//等于-1的情况下只有预定是
						hotelOrder.setStatus(hotelOrder.getStatus() * (-1));
					}*/
							//多房预定的情况下  只退一间房
							if(hotelOrder.getFlag().equals("DUO")){
								if(StringUtils.isNotEmpty(hotelOrder.getRoomIdList())){
									hotelOrder.setRoomIdList(hotelOrder.getRoomIdList().replace(hotelOrder.getRoomId()+",",""));
								}
								saveStatus(hotelOrder.getHotelId(),hotelOrder.getRoomId(),hotelOrder.getRoomTypeId(),day,status,isUpdateOk);
							}else{
								//如果退房订单来源于微信
								if(hotelOrder.getOrderSrc().equals("WX")){
									String [] roomIdList  = hotelOrder.getRoomIdList().split(",");
									for(String roomId : roomIdList){
										saveStatus(hotelOrder.getHotelId(),Long.parseLong(roomId),hotelOrder.getRoomTypeId(),day,status,isUpdateOk);
									}
									//如果退房订单来源于酒店预定
								}else{
									if(hotelOrder.getRoomTotalNum() > 1){
										String [] roomIdList  = hotelOrder.getRoomIdList().split(",");
										for(String roomId : roomIdList){
											saveStatus(hotelOrder.getHotelId(),Long.parseLong(roomId),hotelOrder.getRoomTypeId(),day,status,isUpdateOk);
										}
									}else{
										saveStatus(hotelOrder.getHotelId(),hotelOrder.getRoomId(),hotelOrder.getRoomTypeId(),day,status,isUpdateOk);
									}
								}
							}
						}
						//普通办理入住
						else{
							//多人入住
							if(hotelOrder.getRoomNumber() > 1){
								int finalStatus = status;
						/*hotelOrder.getHotelRooms().stream().forEach(hotelRoom ->{
							saveStatus(hotelRoom.getHotelId(),hotelRoom.getId(),hotelRoom.getTypeId(),day,finalStatus,isUpdateOk);
						});*/
								String [] roomIdList  = hotelOrder.getRoomIdList().split(",");
								for(String roomId : roomIdList){
									saveStatus(hotelOrder.getHotelId(),Long.parseLong(roomId),hotelOrder.getRoomTypeId(),day,status,isUpdateOk);
								}
							}else{
								saveStatus(hotelOrder.getHotelId(),hotelOrder.getRoomId(),hotelOrder.getRoomTypeId(),day,status,isUpdateOk);
							}
						}
					}
				});
				//郝江波改后增加的代码
				if(hotelOrder.getStatus().intValue() <= 0){
					//等于-1的情况下只有预定是
					hotelOrder.setStatus(hotelOrder.getStatus() * (-1));
				}
				save(hotelOrder);
				if(!isUpdateOk.get()){
					throw  new RuntimeException("房态修改失败");
				}
				return 0;
			/*}finally {
				LOCK.unlock();
			}
		}else{
			throw  new RuntimeException("系统繁忙.请稍后再试");
		}*/

	}

	void saveStatus(Long hotelId,Long roomId, Long  roomTypeId, String day,int finalStatus,AtomicBoolean isUpdateOk){
		int r =  hotelRoomDayStatusService.updateStatusByRoomIdAndDay(roomId,
				day,
				finalStatus,
				finalStatus
		);
		if(r == 0){
			HotelRoomDayStatus	 hotelRoomDayStatus =  hotelRoomDayStatusService.getByRoomIdAndDay(roomId, day);
			if(null != hotelRoomDayStatus){
				isUpdateOk.set(false);
			}else{
				hotelRoomDayStatus = new HotelRoomDayStatus();
				try {
					hotelRoomDayStatus.setDay(DateUtils.pasre(day,DateUtils.DATE_PATTERN));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				hotelRoomDayStatus.setStatus(finalStatus);
				hotelRoomDayStatus.setHotelId(hotelId);
				hotelRoomDayStatus.setRoomId(roomId);
				hotelRoomDayStatus.setRoomTypeId(roomTypeId);
				hotelRoomDayStatusService.save(hotelRoomDayStatus);
			}
		}
	}

}
/*int r =  hotelRoomDayStatusService.updateStatusByRoomIdAndDay(hotelOrder.getRoomId(),
							day,
							status
							);
					if(r == 0){
						HotelRoomDayStatus	 hotelRoomDayStatus =  hotelRoomDayStatusService.getByRoomIdAndDay(hotelOrder.getRoomId(), day);
						if(null != hotelRoomDayStatus){
							isUpdateOk.set(false);
						}else{
							hotelRoomDayStatus = new HotelRoomDayStatus();
							hotelRoomDayStatus.setDay(item);
							hotelRoomDayStatus.setStatus(status);
							hotelRoomDayStatus.setHotelId(hotelOrder.getHotelId());
							hotelRoomDayStatus.setRoomId(hotelOrder.getRoomId());
							hotelRoomDayStatus.setRoomTypeId(hotelOrder.getRoomTypeId());
							hotelRoomDayStatusService.save(hotelRoomDayStatus);
						}
					}*/